package api.model;

import m.common.model.DictionaryModel;
import m.common.model.FieldMeta;
import m.common.model.Model;
import m.common.model.TableMeta;
import m.common.model.config.ModelConfig;
import m.common.model.type.FieldType;
import m.common.model.util.ModelQueryList;
import m.system.cache.FlushCache;
import m.system.util.ClassUtil;
import m.system.util.StringUtil;

import java.util.List;

@TableMeta(name="ot_api_table",description="数据表")
public class ApiTable extends Model implements FlushCache, DictionaryModel {

	@FieldMeta(name="name",type=FieldType.STRING,length=50,description="名称")
	private String name;
	@FieldMeta(name="description",type=FieldType.STRING,length=200,description="描述")
	private String description;
	@FieldMeta(name="source",type=FieldType.STRING, description="来源",dictOption = "Y:表,N:自建")
	private String source;
	@FieldMeta(name="source_class",type=FieldType.STRING,length=100,description="来源表")
	private String sourceClass;
	@FieldMeta(name="source_name",type=FieldType.STRING,length=50,description="来源表名")
	private String sourceName;
	@FieldMeta(name="source_desc",type=FieldType.STRING,length=100,description="来源表描述")
	private String sourceDesc;
	@FieldMeta(name="isinit",type=FieldType.STRING, description="是否初始化|Y不能改",dictOption = "Y:是,N:否")
	private String isinit;

	public String getSourceDesc() {
		return sourceDesc;
	}

	public void setSourceDesc(String sourceDesc) {
		this.sourceDesc = sourceDesc;
	}

	public String getSourceName() {
		return sourceName;
	}

	public void setSourceName(String sourceName) {
		this.sourceName = sourceName;
	}

	public String getSourceClass() {
		return sourceClass;
	}

	public void setSourceClass(String sourceClass) {
		this.sourceClass = sourceClass;
	}

	public String getIsinit() {
		return isinit;
	}

	public void setIsinit(String isinit) {
		this.isinit = isinit;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getSource() {
		return source;
	}

	public void setSource(String source) {
		this.source = source;
	}

	public String oidField() {
		if(!StringUtil.isSpace(this.sourceClass)){
			try {
				TableMeta tm = ModelConfig.getTableMeta((Class<Model>) ClassUtil.getClass(this.sourceClass));
				return tm.oidField();
			} catch (ClassNotFoundException ignored) {}
		}
		return "oid";
	}
	public boolean oidIncrement() {
		if(!StringUtil.isSpace(this.sourceClass)){
			try {
				TableMeta tm = ModelConfig.getTableMeta((Class<Model>) ClassUtil.getClass(this.sourceClass));
				return tm.oidIncrement();
			} catch (ClassNotFoundException ignored) {}
		}
		return false;
	}
	/**
	 * oid 表名
	 */
	@Override
	public ApiTable getCacheModel(String key) throws Exception {
		ApiTable model=ModelQueryList.getModel(ApiTable.class,key,"*");
		if(null!=model){
			if(model.getIsinit().equals("Y")){
				return model;
			}
		}else{
			ApiCollect collect=ModelQueryList.getModel(ApiCollect.class,key,"*");
			if(null!=collect) {
				model = new ApiTable();
				model.setOid(key);
				model.setName(collect.getTitle());
				model.setDescription(collect.getDescription());
				model.setSource("C");
			}else {
				TableMeta meta = ModelConfig.getTableMeta(ClassUtil.getClass(Model.class, key));
				if (null != meta) {
					model = new ApiTable();
					model.setOid(key);
					model.setName(meta.name());
					model.setDescription(meta.description());
					model.setSource("Y");
					model.setSourceClass(key);
					model.setSourceName(meta.name());
					model.setSourceDesc(meta.description());
				}
			}
		}
		return model;
	}

	@Override
	public String getDictValue() {
		return this.getOid();
	}

	@Override
	public String getDictName() {
		return this.description+this.name;
	}

	@Override
	public String getDictParentOid() {
		return "";
	}

	@Override
	public String getDictStatus() {
		return "Y";
	}
	@Override
	public List<ApiTable> getCacheList(String key) throws Exception {
		return ModelQueryList.getModelList(ApiTable.class,"*");
	}
}
